package com.nest.WorkNest.act;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nest.WorkNest.MyApplication;
import com.nest.WorkNest.R;
import com.nest.WorkNest.config.Config;
import com.nest.WorkNest.config.UserInfo;
import com.nest.WorkNest.tools.FileHelper;
import com.nest.WorkNest.tools.PhoneHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * 所有的activity继承BaseActivity
 *
 * @author suligen
 */
public class BaseActivity extends Activity {

    protected TextView actionbar_side_name, tv_actionbar_side_right;
    protected LinearLayout ll_tv_actionbar_side_right, ll_tv_actionbar_side_left,
            ll_iv_actionbar_side_right, ll_iv_actionbar_side_left, ll_btn_actionbar_tab_side_right,
            ll_iv_actionbar_tab_side_left, ll_actionbar_side_left;
    protected TextView actionbar_side_tab_name, main_top_save;
    protected TextView tv_actionbar_tab_side_right, tv_actionbar_side_left;
    protected Button btn_actionbar_tab_side_right;
    protected ImageView iv_actionbar_tab_side_left, iv_actionbar_side_right;
    protected int width;
    protected FileHelper fileHelper, fileHelperCache;
    private Toast mToast;
    protected static final String APPLICATION_WWW = "application/x-www-form-urlencoded";
    public ProgressDialog pd;
    public boolean isLogin;
    protected InputMethodManager manager;
    protected MyApplication myApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initstore();
        // 获取屏幕的宽度
        width = PhoneHelper.getScreenWidth(this);
        // myApplication=(MyApplication)getApplication();
    }

    /**
     * 设置共用样式
     *
     * @param isShowTitleBar 显示标题
     * @param layoutId       布局id
     */
    protected void setContentView(boolean isShowTitleBar, int layoutId) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        if (isShowTitleBar) {
            this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(layoutId);
        } else {
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(layoutId);
        }
    }

    public void initBar() {
        isLogin = fileHelper.getBoolean(UserInfo.USERINFO_ISLOGIN);
        tv_actionbar_side_right = (TextView) findViewById(R.id.tv_actionbar_side_right);
        actionbar_side_name = (TextView) findViewById(R.id.actionbar_side_name);
        actionbar_side_tab_name = (TextView) findViewById(R.id.actionbar_tab_side_name);
        tv_actionbar_tab_side_right = (TextView) findViewById(R.id.tv_actionbar_tab_side_right);
        iv_actionbar_tab_side_left = (ImageView) findViewById(R.id.iv_actionbar_tab_side_left);
        tv_actionbar_side_left = (TextView) findViewById(R.id.tv_actionbar_side_left);
        ll_tv_actionbar_side_right = (LinearLayout) findViewById(R.id.ll_tv_actionbar_side_right);
        ll_tv_actionbar_side_left = (LinearLayout) findViewById(R.id.ll_tv_actionbar_side_left);
        ll_iv_actionbar_side_right = (LinearLayout) findViewById(R.id.ll_iv_actionbar_side_right);
        btn_actionbar_tab_side_right = (Button) findViewById(R.id.btn_actionbar_tab_side_right);
        ll_iv_actionbar_side_left = (LinearLayout) findViewById(R.id.ll_iv_actionbar_side_left);
        ll_actionbar_side_left = (LinearLayout) findViewById(R.id.ll_actionbar_side_left);
        main_top_save = (TextView) findViewById(R.id.main_top_save);
        ll_btn_actionbar_tab_side_right = (LinearLayout) findViewById(
                R.id.ll_btn_actionbar_tab_side_right);
        ll_iv_actionbar_tab_side_left = (LinearLayout) findViewById(
                R.id.ll_iv_actionbar_tab_side_left);
        iv_actionbar_side_right = (ImageView) findViewById(R.id.iv_actionbar_side_right);
    }

    public void initProgress(Context context, String tips) {
        pd = new ProgressDialog(context, ProgressDialog.THEME_HOLO_LIGHT);
        pd.setMessage(tips);
        pd.setCanceledOnTouchOutside(false);
        pd.setCancelable(false);
    }

    public void myMsg(final Context context) {
        btn_actionbar_tab_side_right.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyMsgAct.class);
                startActivity(intent);
            }
        });
    }

    public void tabBack() {
        ll_iv_actionbar_tab_side_left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void back() {
        ll_actionbar_side_left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化sharedpeforence
     */
    private void initstore() {
        fileHelper = new FileHelper(getBaseContext(), UserInfo.USER_INFO);
        fileHelperCache = new FileHelper(getBaseContext(), Config.file_temporary);
    }

    /**
     * Toast
     *
     * @param message
     */
    public void showToast(Object message) {

        if (!TextUtils.isEmpty(String.valueOf(message))) {
            if (null == mToast) {

                if (message instanceof Integer) {
                    mToast = Toast.makeText(this, (Integer) message, Toast.LENGTH_SHORT);
                } else if (message instanceof String) {
                    mToast = Toast.makeText(this, (String) message, Toast.LENGTH_SHORT);
                }
            } else {

                if (message instanceof Integer) {
                    mToast.setText((Integer) message);
                } else if (message instanceof String) {
                    mToast.setText((String) message);
                }

            }
            mToast.show();

        }

    }

    /**
     * 判断网络是否链接
     *
     * @param paramActivity
     * @return
     */
    protected boolean hasInternet(Activity paramActivity) {
        ConnectivityManager localConnectivityManager = (ConnectivityManager) paramActivity
                .getApplicationContext().getSystemService("connectivity");
        boolean ret = false;
        if (localConnectivityManager == null) {
            return ret;
        }
        NetworkInfo[] arrayOfNetworkInfo = localConnectivityManager.getAllNetworkInfo();
        if (arrayOfNetworkInfo != null)
            ;
        for (int j = 0; j < arrayOfNetworkInfo.length; j++) {
            if (arrayOfNetworkInfo[j].getState() == NetworkInfo.State.CONNECTED) {
                ret = true;
                break;
            }
        }
        return ret;
    }

    /**
     * intent传值获取String
     *
     * @param TAG
     * @return
     */
    protected String getStringExtra(String TAG) {
        String result = "";
        try {
            result = getIntent().getStringExtra(TAG);
        } catch (Exception e) {
            // TODO: handle exception
            result = "";
        }
        if (result == null) {
            result = "";
        }

        return result;
    }

    /**
     * 清除登录信息
     */
    protected void clearLogininfo() {

        fileHelper.putBoolean(UserInfo.USERINFO_ISLOGIN, false);

        FileHelper.deletedSpFile(this, UserInfo.USER_INFO);
        // FileHelper.deletedSpFile(this, FileHelper.file_userConfig);
        // FileHelper.deletedSpFile(this, FileHelper.file_userAddress);
        // ActivityManager am = (ActivityManager)
        // getSystemService(ACTIVITY_SERVICE);
        // am.killBackgroundProcesses(getPackageName());
    }

    @Override
    protected void onResume() {
        MobclickAgent.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        MobclickAgent.onPause(this);
        super.onPause();
        //注：回调 2
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        return super.dispatchTouchEvent(event);
    }

}
